// pages/compony-pages/compony-info/compony-info.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        componyName:'',
        componyTrade:'',
        finance:'',
        scale:'',
        enterpriseLogo:'',
        teamDes:'',
        address:'',
        isVideo: false,
        mediaCover: '',
        position:1,
        pageSize: 10,
        pageIndex: -1,
        hasMore: true,
        jobList: [],
        enterpriseId: '',
        userId: ''
    },
    previewImg(){
        wx.previewImage({
            current: this.data.mediaCover,
            urls: [this.data.mediaCover]
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let that = this;
        let obj = JSON.parse(options.parmas);
        that.getEnterpriseInfoById(obj.userId , function(){
            let address = app.globalData.enterpriseInfo.Province + app.globalData.enterpriseInfo.City + app.globalData.enterpriseInfo.District + app.globalData.enterpriseInfo.Address;
            let mediaCover = app.globalData.enterpriseInfo.ImageCover;
            let isVideo = false;
            if (mediaCover && mediaCover.substring(mediaCover.lastIndexOf('.') + 1) == 'mp4') {
                isVideo = true;
            }
            that.setData({
                componyName: app.globalData.enterpriseInfo.EnterpriseName,
                componyTrade: app.globalData.enterpriseInfo.IndustryName,
                finance: app.globalData.enterpriseInfo.FinancingStage,
                scale: app.globalData.enterpriseInfo.Scale,
                enterpriseLogo: app.globalData.enterpriseInfo.EnnterpriseLogo,
                teamDes: app.globalData.enterpriseInfo.TeamDes,
                address: address,
                isVideo: isVideo,
                mediaCover: mediaCover,
                enterpriseId: app.globalData.enterpriseInfo.Id,
                userId: obj.userId
            });
        })
        
    },
    getEnterpriseInfoById(id,func) {
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        wx.request({
            url: app.globalData.serverAddress.getEnterpriseInfoById,
            data: {
                userInfoId: id
            },
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function (res) {
                console.log('getEnterpriseInfoById success', res);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        let verifyStatus = res.data.BodyContent.VerifyStatus;
                        if (verifyStatus == 2) {
                            app.globalData.enterpriseInfo = res.data.BodyContent;
                        }
                        if (func) {
                            func(verifyStatus);
                        }
                    } else {
                        func(0);
                    }
                } else {
                    // Toast("请求")
                }
            },
            fail: function (res) { console.log("getEnterpriseInfoById fail", res) },
            complete: function (res) { wx.hideLoading(); },
        })
    },
    clickItem: function (event) {
        let index = event.target.id;
        this.setData({
            position:index
        });
        if (index == 2 && this.data.jobList.length <= 0){
            this.getEnterpriseJobList();
        }
    },
    selectItem(e) {
        let index = e.currentTarget.dataset.index;
        let parmas = {
            jobId: this.data.jobList[index].Id,
            jobName: this.data.jobList[index].JobName,
            flag: 2
        }
        wx.navigateTo({
            url: '/pages/compony-pages/job-detail/job-detail?parmas=' + JSON.stringify(parmas)
        });
    },
    getEnterpriseJobList() {
        let that = this;
        if (!that.data.hasMore) return;
        wx.showLoading({
            title: '加载中...',
            mask: true
        });
        let pageI = ++that.data.pageIndex;
        let data = {
            pageSize: that.data.pageSize,
            pageIndex: pageI,
            currentUserInfoId: that.data.userId,
            enterpriseInfoId: that.data.enterpriseId
        };
        that.setData({
            pageIndex: pageI
        });
        wx.request({
            url: app.globalData.serverAddress.getEnterpriseJobs,
            data: data,
            header: {
                'Authorization': "Bearer " + app.globalData.token
            },
            method: 'GET',
            success: function (res) {
                console.log(res.data.BodyContent);
                if (res.statusCode == 200) {
                    if (res.data.CodeStatus == 20000) {
                        let count = res.data.BodyContent.TotalCount;
                        let flag = (that.data.pageIndex + 1) * that.data.pageSize < count;
                        // console.log(flag);
                        var newList = that.data.jobList.concat(res.data.BodyContent.List);
                        newList.forEach(function (arr, index) {
                            newList[index].salaryTime = parseInt((arr.SalaryMin * 1000) / 80);
                        });
                        that.setData({
                            jobList: newList,
                            hasMore: flag
                        });
                    }
                    else {
                        that.setData({
                            hasMore: false
                        });
                        let msg = res.data.Message;
                    }
                }
            },
            fail: function (res) { console.log("getEnterpriseJobList fail", res) },
            complete: function (res) { wx.hideLoading() },
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        if(this.data.position == 2){
            this.getEnterpriseJobList();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})